package com.service;

import android.*;
import android.R;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;

import com.service.utills.DataTransfer;
import com.service.utills.JSONConverter;

/**
 * Created by rora0613 on 25.09.13.
 */
public class RegistrationActivity extends PreferenceActivity {

    private DataTransfer dataTransfer = new DataTransfer();
    private JSONConverter jsonConverter = new JSONConverter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(R.id.content, new RegistrationPreferenceFragment()).commit();

    }

    public class RegistrationPreferenceFragment extends PreferenceFragment {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(com.service.R.xml.prefs_registration);
            Preference confirmSignButton = (Preference) findPreference("confirmButton");
            if (confirmSignButton!=null) {
               confirmSignButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){
                   @Override
                   public boolean onPreferenceClick(Preference arg0) {
                       //sending login inform to the server
                       PizzaApplication pizzaApplication = (PizzaApplication) getApplication();
                       dataTransfer.sendDateToServer(jsonConverter.setJsonLogin(pizzaApplication.getLogin()));
                       //TODO:need to implement logic for checking login and password on the server
                       boolean loginRight = true;
                       if (loginRight) {
                           Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
                           RegistrationActivity.this.startActivity(intent);
                       }
                       return true;
                   }
               });
            }
        }


    }
}
